package com.example.lenovo.wechattest;

import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ImageButton;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity implements View.OnClickListener{

    private ImageButton weChatButton;
    private ImageButton contactButton;
    private ImageButton findButton;
    private ImageButton meButton;

    private TextView weChatText;
    private TextView contactText;
    private TextView findText;
    private TextView meText;

    private WeChatFragment weChatFragment = null;
    private ContactFragment contactFragment = null;
    private FindFragment findFragment = null;
    private MeFragment meFragment = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        weChatButton = findViewById(R.id.btn_wechat);
        contactButton = findViewById(R.id.btn_contact);
        findButton = findViewById(R.id.btn_find);
        meButton = findViewById(R.id.btn_me);

        weChatText = findViewById(R.id.text_wechat);
        contactText = findViewById(R.id.text_contact);
        findText = findViewById(R.id.text_find);
        meText = findViewById(R.id.text_me);

        weChatButton.setOnClickListener(this);
        contactButton.setOnClickListener(this);
        findButton.setOnClickListener(this);
        meButton.setOnClickListener(this);

        selectTab(0);

    }

    @Override
    public void onClick(View v) {
        setTabDefaultColor();
        switch (v.getId()) {
            case R.id.btn_wechat:
                weChatButton.setImageResource(R.mipmap.weixin_pressed);
                weChatText.setTextColor(0xFF45C01A);
                selectTab(0);
                break;
            case R.id.btn_contact:
                contactButton.setImageResource(R.mipmap.contact_list_pressed);
                contactText.setTextColor(0xFF45C01A);
                selectTab(1);
                break;
            case R.id.btn_find:
                findButton.setImageResource(R.mipmap.find_pressed);
                findText.setTextColor(0xFF45C01A);
                selectTab(2);
                break;
            case R.id.btn_me:
                meButton.setImageResource(R.mipmap.profile_pressed);
                meText.setTextColor(0xFF45C01A);
                selectTab(3);
                break;
                default:
                    break;
        }
    }

    private void setTabDefaultColor() {
        weChatButton.setImageResource(R.mipmap.weixin_normal);
        contactButton.setImageResource(R.mipmap.contact_list_normal);
        findButton.setImageResource(R.mipmap.find_normal);
        meButton.setImageResource(R.mipmap.profile_normal);

        weChatText.setTextColor(0xFFAAAAAA);
        contactText.setTextColor(0xFFAAAAAA);
        findText.setTextColor(0xFFAAAAAA);
        meText.setTextColor(0xFFAAAAAA);
    }

    public void selectTab(int index) {
        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();

        if (weChatFragment != null) {
            transaction.hide(weChatFragment);
        }
        if (contactFragment != null) {
            transaction.hide(contactFragment);
        }
        if (findFragment != null) {
            transaction.hide(findFragment);
        }
        if (meFragment != null) {
            transaction.hide(meFragment);
        }
        switch (index) {
            case 0:
                if (weChatFragment == null) {
                    weChatFragment = new WeChatFragment();
                    transaction.add(R.id.fragmentTest, weChatFragment);
                } else {
                    transaction.show(weChatFragment);
                }
                break;
            case 1:
                if (contactFragment == null) {
                    contactFragment = new ContactFragment();
                    transaction.add(R.id.fragmentTest, contactFragment);
                } else {
                    transaction.show(contactFragment);
                }
                break;
            case 2:
                if (findFragment == null) {
                    findFragment = new FindFragment();
                    transaction.add(R.id.fragmentTest, findFragment);
                } else {
                    transaction.show(findFragment);
                }
                break;
            case 3:
                if (meFragment == null) {
                    meFragment = new MeFragment();
                    transaction.add(R.id.fragmentTest, meFragment);
                } else {
                    transaction.show(meFragment);
                }
                break;
                default:
                    Log.e("MainActivity", "on click error!");
        }
        transaction.commit();
    }

}
